package com.jack.leetcode.eachproblem_2020;

public class 颜色分类 {

    public void sortColors(int[] nums) {
        if(nums == null){
            return;
        }
        int n = nums.length;
        int l = 0, r = n - 1;
        for(int i = 0;i < r;i++){
            while(i < r && nums[i] == 2){
                trans(nums, i, r);
                r--;
            }
            if(nums[i] == 0){
                trans(nums, l, i);
                l++;
            }
        }
    }

    private void trans(int[] nums, int l, int r){
        int tmp = nums[l];
        nums[l] = nums[r];
        nums[r] = tmp;
    }
}
